<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="页面标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入页面标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="页面描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入页面描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="页面关键字" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入页面关键字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="页面地址" prop="path">
        <el-input
          v-model="queryParams.path"
          placeholder="请输入页面地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dance_page_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['Dance:page:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="pageList"
      row-key="pageId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="页面标题" prop="title" />
      <el-table-column label="页面描述" align="center" prop="description" />
      <el-table-column label="页面关键字" align="center" prop="keywords" />
      <el-table-column label="页面地址" align="center" prop="path" width="120px"/>
      <el-table-column label="状态" align="center" prop="status" width="80px">
        <template #default="scope">
          <dict-tag :options="dance_page_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderBy" width="60px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['Dance:page:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['Dance:page:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['Dance:page:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改页面管理对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="pageRef" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="页面标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入页面标题" />
        </el-form-item>
        <el-form-item label="页面描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入页面描述" />
        </el-form-item>
        <el-form-item label="页面关键字" prop="keywords">
          <el-input v-model="form.keywords" placeholder="请输入页面关键字" />
        </el-form-item>
        <el-form-item label="页面地址" prop="path">
          <el-input v-model="form.path" placeholder="请输入页面地址" />
        </el-form-item>
        <el-form-item label="父页面" prop="parentId">
          <el-tree-select
            v-model="form.parentId"
            :data="pageOptions"
            :props="{ value: 'pageId', label: 'title', children: 'children' }"
            value-key="pageId"
            placeholder="请选择父页面"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="排序" prop="orderBy">
          <el-input-number v-model="form.orderBy" controls-position="right"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dance_page_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Page">
import { listPage, getPage, delPage, addPage, updatePage } from "@/api/Dance/page";

const { proxy } = getCurrentInstance();
const { dance_page_status } = proxy.useDict('dance_page_status');

const pageList = ref([]);
const pageOptions = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    title: undefined,
    description: undefined,
    keywords: undefined,
    path: undefined,
    status: undefined,
  },
  rules: {
    pageId: [
      { required: true, message: "编号不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "页面标题不能为空", trigger: "blur" }
    ],
    // description: [
    //   { required: true, message: "页面描述不能为空", trigger: "blur" }
    // ],
    // keywords: [
    //   { required: true, message: "页面关键字不能为空", trigger: "blur" }
    // ],
    path: [
      { required: true, message: "页面地址不能为空", trigger: "blur" }
    ],
    parentId: [
      { required: true, message: "父编号不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询页面管理列表 */
function getList() {
  loading.value = true;
  listPage(queryParams.value).then(response => {
    pageList.value = proxy.handleTree(response.data, "pageId", "parentId");
    loading.value = false;
  });
}

/** 查询页面管理下拉树结构 */
function getTreeselect() {
  listPage().then(response => {
    pageOptions.value = [];
    const data = { pageId: 0, title: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "pageId", "parentId");
    pageOptions.value.push(data);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    pageId: null,
    title: null,
    description: null,
    keywords: null,
    path: null,
    parentId: null,
    status: '2',
    orderBy: 0,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("pageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.pageId) {
    form.value.parentId = row.pageId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加页面管理";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  loading.value = true;
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.parentId;
  }
  getPage(row.pageId).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改页面管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pageRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.pageId != null) {
        updatePage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addPage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除页面管理编号为"' + row.pageId + '"的数据项？').then(function() {
    loading.value = true;
    return delPage(row.pageId);
  }).then(() => {
    loading.value = false;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

getList();
</script>
